<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('dzd.quick Search Fields') })"
        >
        <el-button v-blur class="table-header-operate" type="primary">
            <span class="table-header-operate-text" @click="handleOperate">新增JOB NO</span>
        </el-button>
        <el-button v-blur class="table-header-operate" type="primary" :disabled="!state.canPrintSheet">
            <span class="table-header-operate-text" @click="handlePrint('12')">打印JOBSHEET</span>
        </el-button>
        <el-button v-blur class="table-header-operate" type="primary"  :disabled="!state.canPrintAmend">
            <span class="table-header-operate-text" @click="handlePrint('15')">打印AMENDMENT</span>
        </el-button>
        </TableHeader>

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />
        <!-- 表单 -->
        <el-dialog 
            class="ba-operate-dialog" 
            :close-on-click-modal="false" 
            :model-value="baTable.form.operate=='add' ? true : false" 
            :title="'新增JOB NO'"
            @close="baTable.toggleForm">
            <el-tabs v-model="state.activeName" v-if="baTable.form.operate=='add'" class="demo-tabs" >
                <el-tab-pane label="应收对账单" :name="0">
                    <PopupFormAdd :type="0" />
                </el-tab-pane>
                <el-tab-pane label="仅应付更正单" :name="1">
                    <PopupFormAdd :type="1" />
                </el-tab-pane>
            </el-tabs>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { dzd } from '/@/api/controllerUrls'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupFormView from './popupFormView.vue'
import PopupFormAdd from './popupFormAdd.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'



const state = reactive({
    activeName:0,
    jobno:'',
    canPrintSheet:false,
    canPrintAmend:false
})

const { t } = useI18n()
const tableRef = ref()
const optButtons = defaultOptButtons(["edit","delete"])
const baTable = new baTableClass(
    new baTableApi(dzd,'_job'),
    {
        pk: 'ID',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('dzd.DZDNO'), prop: 'DZDNO', show:false },
            { label: t('dzd.JOBNO'), prop: 'JOBNO', align: 'center' },
            { label: '来往单位', prop: 'CORPCODE', align: 'center' },
            { label: '费用类型', prop: 'BILLCLASS', align: 'center', render:'tag',replaceValue: { '0': '正常','1':'代垫','2':'更正' } },
            { label: 'JOB制单日期', prop: 'JOBDATE', align: 'center',render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            { label: '对账日期', prop: 'DZDATE', align: 'center',render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            { label: '应收人民币', prop: 'YSRMB', align: 'center',operator: false  },
            { label: '应收美金', prop: 'YSUSD', align: 'center',operator: false  },
            { label: '应付人民币', prop: 'DZOP', align: 'center' ,operator: false },
            { label: '应付美金', prop: 'YFRMB', align: 'center',operator: false  },
            { label: '折合人民币', prop: 'ZHRMB', align: 'center',operator: false },
        ],
        dblClickNotEditColumn: [undefined, ],
        defaultOrder: { prop: 'ID', order: 'desc' },
        showComSearch:true
    },
    {
        defaultItems: [],
    },
    {
        onTableDblclick:(table:any)=>{
            baTable.form.operate = 'view'
            baTable.form.items = table.row
            return false
        },
        onTableAction:(table:any)=>{
            if(table.event == 'selection-change'){
                if(table.data.length){
                    state.canPrintSheet = true
                    state.canPrintAmend = true
                    state.jobno = table.data[0].JOBNO.split('-')[0]
                    console.log(state.jobno)
                    let amendNum = 0
                    table.data.map((item:any)=>{
                        if(item.JOBNO.indexOf(state.jobno)<0 ){
                            ElMessage.warning('仅可选择前12位JOBNO相同单子')
                            state.canPrintSheet = false
                            state.canPrintAmend = false
                            return
                        }
                        // 含有正常账单时，打印更正无效
                        if(item.BILLCLASS =='0') state.canPrintAmend = false
                        if(item.BILLCLASS =='2'){
                            amendNum += 1
                        }
                    })
                    //全是更正单时 jobsheet无效
                    if(amendNum == table.data.length) state.canPrintSheet = false
                }else{
                    state.canPrintSheet = false
                    state.canPrintAmend = false
                }

            }
            return true
        }
    }
)

provide('baTable', baTable)


onMounted(() => {
    baTable.table.ref = tableRef.value
    
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})

const handleOperate = ()=>{
    baTable.form.operate = 'add'
}

const handlePrint = (type:string)=>{
    let selectList = baTable.table.selection
    if(selectList&&selectList.length){
        // window.open(`/#/admin/print?type=${type}&tzid=${selectList[0].JOBNO}`)
        window.open(`/#/admin/print?type=${type}&tzid=${selectList.map((item:any)=>item.JOBNO).join(',')}`)
    }else{
        ElMessage.warning('请选择需打印的单据！')
    }
}



</script>

<script lang="ts">
import { defineComponent } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
export default defineComponent({
    name: 'dzd',
})
</script>

<style scoped lang="scss">
:deep(.el-table__header) tr th .el-checkbox{
    display: none;
}

</style>
